from urllib.parse import urljoin
from datetime import datetime, timedelta
from playwright.sync_api import Playwright, sync_playwright, expect, Browser, BrowserContext as Context
from maillog import check_and_send_email


def watchdrc(browser: Browser, context: Context) -> None:
    print("DRC...")
    page = context.new_page()
    page.goto("https://www.drc.gov.cn/default.aspx")
    page.wait_for_timeout(2000)
    absolute_url = ''
    first_link = page.query_selector('a.ywdtfirst.titleblack')
    title=''
    if first_link:
        href_value = first_link.get_attribute('href')
        if href_value:
            # 如果 href 是相对地址，则加上域名
            absolute_url = urljoin("https://www.drc.gov.cn", href_value)
            title = first_link.inner_text()
            # print(f"Absolute URL: {absolute_url}")
        else:
            print("No href value found.")
        first_link.click()
    else:
        print("No first link found.")


    page.goto(absolute_url)
    page.wait_for_timeout(2000)
    # Get the time from the span element
    time_span = page.query_selector('span#MainContent_docAuthor')
    if time_span:
        time_str = time_span.text_content().strip()
        # print(f"Extracted Time: {time_str}")

        # Parse the time string into a datetime object
        try:
            post_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M")
        except ValueError as e:
            print(f"Error parsing time: {e}")
            return

        check_and_send_email('drc', '国研中心网站', title, post_time, absolute_url)
    else:
        print("没有时间标签.")


def watchxinhua(browser: Browser, context: Context) -> None:
    print("新华社...")
    page = context.new_page()
    page.goto("https://www.news.cn/")
    page.wait_for_timeout(2000)

    # 选择包含新闻标题的<a>标签
    news_link = page.query_selector('div#headline a')

    if news_link:
        href_value = news_link.get_attribute('href')
        title = news_link.inner_text()

        # print(f"Link: {href_value}")
        # print(f"Title: {title}")

        # 如果需要点击该链接并访问详细页面，可以使用以下代码
        page.goto(href_value)
        page.wait_for_timeout(1000)
        
        # 使用XPath选择器获取包含时间信息的元素
        time_div = page.query_selector('div.header-time.left')
        if time_div:
            time_str = time_div.text_content().strip()
            # print(f"time_div content is: {time_str}")

            # 解析为datetime对象
            try:
                post_time = datetime.strptime(time_str, "%Y %m/%d %H:%M:%S")
                # print(f"Parsed Time: {post_time}")
            except ValueError as e:
                print(f"Error parsing time: {e}")
                return

            check_and_send_email('xinhua', '新华网', title, post_time, href_value)
        else:
            print("No time div found.")
            
    else:
        print("No news link found.")

def watchpeople(browser: Browser, context: Context) -> None:
    print("人民网...")
    page = context.new_page()
    page.goto("http://www.people.com.cn/")
    page.wait_for_timeout(2000)

    # 选择包含新闻标题的<a>标签
    news_link = page.query_selector('h1#rm_topline a')

    if news_link:
        href_value = news_link.get_attribute('href')
        # print(f"Link: {href_value}")

        # 如果需要点击该链接并访问详细页面，可以使用以下代码
        page.goto(href_value)
        page.wait_for_timeout(1000)
        
        # 提取时间信息
        time_div = page.query_selector('div.col-1-1')
        if time_div:
            time_str = time_div.text_content().strip()
            # print(f"Extracted Time: {time_str}")

            # 移除多余的文本，只保留日期和时间部分
            time_only = time_str.split('|')[0].strip()

            # 解析为datetime对象
            try:
                post_time = datetime.strptime(time_only, "%Y年%m月%d日%H:%M")
                # print(f"Parsed Time: {post_time}")
            except ValueError as e:
                print(f"Error parsing time: {e}")
                return
            # Get the current time
            current_time = datetime.now()
            
            # 提取文章标题
            title=''
            title_element = page.locator('div.main div.layout.rm_txt.cf div.col.col-1 h1')
            if title_element:
                title = title_element.text_content().strip()
                # print(f"Title: {title}")
            else:
                print("No title element found.")

            
            check_and_send_email('people', '人民网', title, post_time, href_value)
        else:
            print("No time div found.")
    else:
        print("No news link found.")

# 中国政府网

def watchgov(browser: Browser, context: Context) -> None:
    print("中国政府网...")
    page = context.new_page()
    page.goto("https://www.gov.cn/")
    page.wait_for_timeout(2000)

    # 选择包含新闻标题的<a>标签
    div_toutiao = page.query_selector('div#index_toutiao')
    if not div_toutiao:
        news_link = page.query_selector('div#index_toutiao a')
    
    h2_middle = div_toutiao.query_selector('h2.mhide')
    if h2_middle:
        news_link = div_toutiao.query_selector('a')

    if news_link:
        href_value = news_link.get_attribute('href')
        if href_value:
            # 如果 href 是相对地址，则加上域名
            absolute_url = urljoin("https://www.gov.cn", href_value)
            # print(f"Absolute URL: {absolute_url}")
        else:
            print("No href value found.")

        # 提取内部文字内容
        title = news_link.text_content().strip()
        # print(f"Title: {title}")

        # 如果需要点击该链接并访问详细页面，可以使用以下代码
        page.goto(absolute_url)
        page.wait_for_timeout(1000)

        # 提取时间信息
        time_div = page.query_selector('div.pages-date')
        if time_div:
            time_str = time_div.text_content().strip()

            # 移除多余的文本，只保留日期和时间部分
            time_only = time_str.split('来源： ')[0].strip()

            # 解析为datetime对象
            try:
                post_time = datetime.strptime(time_only, "%Y-%m-%d %H:%M")
                # print(f"Parsed Time: {post_time}")
            except ValueError as e:
                print(f"Error parsing time: {e}")
                return

            check_and_send_email('gov', '中国政府网', title, post_time, absolute_url)
        else:
            print("No time div found.")
    else:
        print("No news link found.")

# 中国经济网

def watchjce(browser: Browser, context: Context) -> None:
    print("中国经济网...")
    page = context.new_page()
    page.goto("http://www.ce.cn/")
    page.wait_for_timeout(2000)

    # 选择包含新闻标题的<a>标签
    div_toutiao = page.query_selector('div.hotnews > dl > dt > span > a')
    if not div_toutiao:
        return
    
    href_value = div_toutiao.get_attribute('href')
    # print(f"href_value: {href_value}")
    page.goto(href_value)
    page.wait_for_timeout(1000)
    # 提取内容
    title_div = page.query_selector('h1#articleTitle')
    if title_div:
        title = title_div.text_content().strip()
        # print(f"Title: {title}")
    # 提取时间信息
    time_div = page.query_selector('span#articleTime')
    if time_div:
        time_str = time_div.text_content().strip()
        # print(f"Extracted Time: {time_str}")

        try:
            post_time = datetime.strptime(time_str, "%Y年%m月%d日 %H:%M")
            # print(f"Parsed Time: {post_time}")
        except ValueError as e:
            print(f"Error parsing time: {e}")
            return

        check_and_send_email('jce', '中国经济网', title, post_time, href_value)
    else:
        print("No time div found.")
    pass

# 光明网

def watchgmw(browser: Browser, context: Context) -> None:
    print("光明网...")
    page = context.new_page()
    page.goto("https://www.gmw.cn/")
    page.wait_for_timeout(2000)
    div_toutiao = page.query_selector('div.m_imgTitleWrap > a')
    if not div_toutiao:
        return
    href_value = div_toutiao.get_attribute('href')

    page.goto(href_value)
    page.wait_for_timeout(1000)

    title_div = page.query_selector('h1.u-title')
    if title_div:
        title = title_div.text_content().strip()
        # print(f"Title: {title}")

    time_div = page.query_selector('span.m-con-time')
    if time_div:
        time_str = time_div.text_content().strip()
        # print(f"Extracted Time: {time_str}")
        try:
            post_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M")
            # print(f"Parsed Time: {post_time}")
        except ValueError as e:
            print(f"Error sending email: {e}")
            return

        check_and_send_email('gmw', '光明网', title, post_time, href_value)
    else:
        print("No time div found.")
    pass


# 写main函数

def main():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        watchdrc(browser, context)
        watchxinhua(browser, context)
        watchpeople(browser, context)
        watchgov(browser, context)
        watchjce(browser, context)
        watchgmw(browser, context)

        # ---------------------
        context.close()
        browser.close()

if __name__ == '__main__':
    main()

